#!/usr/bin/sh

if [ $# -eq 0 ] ; then
  echo "Usage: $0 user:pass@host { remotedir } {localdir} {target_file1,target_file2,target_file3,....}" >&2
  exit -1;
fi

user="$(echo $1 | cut -d: -f1)"
password="$(echo $1 | cut -d@ -f1 | cut -d: -f2)"
server="$(echo $1 | cut -d@ -f2)"

#/u01/oracle/jis/jisdev/drs/scripts/ftpsyn/ftpsyn.sh oracsms:do2known@192.168.204.105 /u01/oracle/jis/jisprod/drs/output/hk/ /u01/oracle/jis/jisdev/drs/output//hk/ C01080214-28.xls,C01080214-28.pdf,


ftpLoginFile="/tmp/ftpLogin.$$"
remoteTMPfile="/tmp/rFilelist.$$"
localTMPfile="/tmp/lFilelist.$$"

logFile="`pwd`/synLog.log"

#echo "logfile : $logFile"

trap "/bin/rm -f $ftpLoginFile" 0 1 15      # zap on exit &sigs
trap "/bin/rm -f $remoteTMPfile" 0 1 15      # zap on exit &sigs
trap "/bin/rm -f $localTMPfile" 0 1 15      # zap on exit &sigs


echo "`date` Passed Initital...enter Local Path : $3 " >> $logFile

cd $3

echo "Geting list..." >> $logFile
#select all file from FTP
echo `ftp -n <<!
open $server
user $user $password
cd $2
nlist
bye
!
` > $remoteTMPfile;
echo "list Complete..." >> $logFile  ;

if [ -e $remoteTMPfile ]; then
   echo "`date` Get Remote Directory [$2] list success.." >> $logFile  ;
else
   echo "`date` Get Remote Directory [$2] list fail! Program Terminate!" >> $logFile
   exit 1;
fi

echo "open $server " > $ftpLoginFile
echo "user $user $password " >> $ftpLoginFile
echo "cd $2" >> $ftpLoginFile


#build target file list

echo "Get target list" >> $logFile

fileList=$4
cnt=1
lp=$(echo $fileList | cut -d, -f$cnt)

lastLP="";

echo $lastLP>$localTMPfile

while [ "$lp" != ""  and "$lastLP" != "$lp" ]
do
echo $lp >> $localTMPfile
echo $lp >> $logFile
cnt=$(($cnt + 1))
lastLP=lp
lp=$(echo $fileList | cut -d, -f$cnt)
done

echo "Get target list complete" >> $logFile

uploadCnt=0
downloadCnt=0


#remote not exists file
for localFile in $(cat $localTMPfile)
do

  hitCnt=0

  for remoteFile in $(cat $remoteTMPfile)
  do
     if [ "$localFile" = "$remoteFile"  ] ; then
        hitCnt=$(($hitCnt+1))
     fi

  done

  if [ $hitCnt -eq 0 ]; then
     echo "put $localFile " >> $ftpLoginFile;
     echo "`date` put $localFile " >> $logFile;
     uploadCnt=$(($uploadCnt+1))
  fi

done


#localFile Not Exists

for remoteFile in $(cat $remoteTMPfile)
do

  hitCnt=0

  for localFile in $(cat $localTMPfile)
  do
     if  [ "$remoteFile" = "$localFile" ];  then
        hitCnt=$(($hitCnt+1))
     fi
  done

  #if remote server have the target file
  if [ $hitCnt -lt 0 ]; then
     echo "get $remoteFile" >> $ftpLoginFile;
     echo "`date` get $localFile " >> $logFile;
     downloadCnt=$(($downloadCnt+1))
  fi

done
    echo "bye" >> $ftpLoginFile

    if [ $(($uploadCnt + $downloadCnt)) -gt 0 ]; then
       ftp -n <  $ftpLoginFile;
       echo "Total : Upload $uploadCnt , Download $downloadCnt file(s)." >> $logFile
    else
       echo "No file to Upload " >> $logFile
    fi

    #cat $logFile

exit $retcode;
